/**
 * 机构编辑窗口
 */
define(function(){
	var DepartWindow = Ext.extend(Ext.Window, {
		modifiedId: null, //从构造器中传入，表示要编辑的对象的id，为空表示新增
		parentDepart: null, //在构造器中传入，表示要编辑对象的父节点
		resizable: false,
		modal: true,
		width: 350,
	    animateTarget: cu.getDefaultAnimateTarget(),
        bodyStyle:'padding:3px;',
		iconCls: 'icon-window',
		initComponent: function(){
			DepartWindow.superclass.initComponent.call(Ext.apply(this, {
				items: [this.formPanel = this.createFormPanel()],
				buttons: this.createButtons(),
				listeners: {
					show: function(){
						this.modifiedId && this.initFormData();
						this.getForm().firstFocus();
						cu.bindEnter(this, this.save);
					}
				}
			}));
		},
		save: function(continueToAdd){
			cu.submit({
				form: this.getForm(), 
				url: 'sys/depart/saveDepart.do',
				success: function(data){
					if (lu.isFn(this.callback)){
						var parentNode = this.getField('parentId').getValueNode();
						this.callback.call(this.cbScope, parentNode.getPath(), data);
					}
					if(continueToAdd){
						this.getField('name').setValue('');
						this.getField('parentId').reload(); //刷新树节点
						this.getForm().clearInvalid();
					}
					else{
						this.close();
					}
				},
				scope: this
			});
		},
		initFormData: function(){
			cu.get({
				url:'sys/depart/getDepart.do', 
				params: {id: this.modifiedId},
				success: function(data){
					this.getForm().setValues(data);
				},
				scope: this
			});
		},
		getForm: function(){
			return this.formPanel.getForm();
		},
		getField: function(name){
			return this.getForm().findField(name);
		},
		createFormPanel: function(){
			var formPanel = new Ext.form.FormPanel({
			    baseCls: 'x-plain',
			    bodyStyle:'padding:5px;',
			    labelWidth: 56,
			    labelPad: 5,
			    labelAlign: "right",
			    defaults: {anchor:'99%', style:'margin-bottom:5px;'},
			    items: [
			        {xtype:'hidden', name:'id'},
			        {
						xtype:'combotree',
						fieldLabel:'父机构',
						hiddenName: 'parentId',
						value: this.parentDepart.id,
						height: 200,
						dataUrl: 'sys/depart/getDepartTreeData.do?',
						nodePathUrl: 'sys/util/getEntityAttrPath.do?c=sys.Department',
						root: {id:'root', text:'根机构', expanded: true},
						rootVisible: true,
						rootSelectable: true,
						rootValue: null,
						showFullPath: true,
						allowBlank: false,
				        validator: function(value){
				        	var selfId = this.ownerCt.getForm().findField('id').getValue();
				        	var parentId = this.ownerCt.getForm().findField('parentId').getValue();
				        	return parentId != selfId ? true : "父机构不能是自身";
				        }
			        },{
						xtype:'textfield',
						name: 'name',
						fieldLabel: '机构名称',
						maxLength: 30, maxLengthText: '机构名称不能超过30个字',
						allowBlank:false
					},{
						xtype: 'numberfield',
						name: 'sortNum',
						fieldLabel: '排序号',
						value: 1
					}
			    ],
			    firstFocus: function(){
			    	this.findField('name').focus(false, 200);
			    }
			});
			return formPanel;
		},
		createButtons: function(){
			var buttons = [];
			if (!this.modifiedId){
				buttons.push({text: '保存并新增', width:90, handler: function(){this.save(true);}, iconCls: 'icon-save', scope: this});
			}
			buttons.push({text: '保存', width:60, handler: function(){this.save(false);}, iconCls: 'icon-save', scope: this});
			buttons.push({text: '关闭', width:60, handler: this.close, iconCls: 'icon-close', scope: this});
			return buttons;
		}
	});
	return DepartWindow;
});